package com.link.base.base.questionsurvey.controller;

import com.link.base.base.user.model.User;
import com.link.base.base.questionsurvey.model.Question;
import com.link.base.base.questionsurvey.model.QuestionAnswer;
import com.link.base.base.questionsurvey.model.QuestionItem;
import com.link.base.base.questionsurvey.service.QuestionService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 问卷调查
 */
@Controller
@RequestMapping("/anonymous/questionsurvey")
public class QuestionAnonymousController extends BasicController<Question> {

    @Resource
    private QuestionService questionService;
    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Override
    public BasicService<Question> getBasicService() throws Exception {
        return questionService;
    }

    @RequestMapping(value = "/queryUserFillInPage")
    @ResponseBody
    public Map<String, Object> queryUserFillInPage(@JsonParam QueryParams qps, @JsonParam User entity, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            User t = (User) BasicModel.transformClass(entity, qps);
            List<User> list = questionService.queryUserFillInPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<User>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
//            result.put("rows", new ArrayList<T>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /** 查询问卷列表*/
    @RequestMapping(value = "/queryQuestionSurveyPage")
    @ResponseBody
    public Map<String, Object> queryList(@JsonParam QueryParams qps, @JsonParam Question entity, HttpSession session, HttpServletRequest request,
                                         HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            Question t = (Question) BasicModel.transformClass(entity.getClass(), qps);
            List<Question> list = questionService.queryQuestionSurveyPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Question>());
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 新建问卷
     */
    @RequestMapping(value = "/questionInsert")
    @ResponseBody
    public Map<String, Object> questionInsert(@JsonParam Question record) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Long quesId = record.getId();
            quesId = keyGenerateDao.keyGenerate();
            record.setId(quesId);
            questionService.questionTitleInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/answerInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> answerInsert(@RequestBody QuestionAnswer record, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            questionService.answerInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /** 添加问卷*/
    @RequestMapping(value = "/addQuestionSurvey", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addQuestionSurvey(@JsonParam Question entity, HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            entity.setId(keyGenerateDao.keyGenerate());
            result.put("success", true);
            result.put("rows", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 添加调查问卷 - 进入调查问卷页面
     */
    @RequestMapping(value = "/addQstSurvey")
    public String addQstSurvey(@JsonParam QueryParams qps, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        try {
            Question question = new Question();
            question.setId(Long.parseLong(""));
            request.setAttribute("question", question);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return "question/creQues";
    }

    /**
     * 创建调查问卷 - 进入下一个问题
     *
     * @param qps
     */
    @RequestMapping(value = "/nextQstSurvey", method = RequestMethod.POST)
    public String nextQstSurvey(@JsonParam Question entity, @JsonParam QueryParams qps,
                                HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        try {
            QuestionItem questionItem = new QuestionItem();
            request.setAttribute("question", entity);
            request.setAttribute("questionItem", questionItem);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return "question/creQueIt";
    }

    /**
     * 创建调查问卷 - 返回上一个问题或者调查问卷页面
     *
     * @param qps
     */
    @RequestMapping(value = "/lastQstSurvey", method = RequestMethod.POST)
    public String lastQstSurvey(@JsonParam Question entity, @JsonParam QueryParams qps,
                                HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        //返回页面
        String url = "";
        try {
            boolean isMainPage = false;
            QuestionItem questionItem = new QuestionItem();
            List<QuestionItem> itemList = entity.getItemList();
            if (itemList != null) {
                int length = itemList.size();
                if (length > 0) {
                    isMainPage = false;
                    questionItem = itemList.get(length - 1);
                    itemList.remove(length - 1);
                } else {
                    isMainPage = true;
                }
            } else {
                isMainPage = true;
            }
            if (isMainPage) {
                //返回创建调查问卷页面
                request.setAttribute("question", entity);
                url = "question/creQues";
            } else {         //返回调查问卷问题页面
                request.setAttribute("question", entity);
                request.setAttribute("questionItem", questionItem);
                url = "question/creQueIt";
            }
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return url;
    }

    @RequestMapping(value = "/saveQuestionSurvey", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveQuestionSurvey(@RequestBody Question entity, HttpSession session, HttpServletRequest request,
                                                  HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            questionService.questionTitleInsert(entity);
            result.put("success", true);
            result.put("rows", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
